package org.hackystat.devcathlon.ui.page.profile;

import java.io.Serializable;
import java.util.List;

import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.engine.user.UserManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.engine.profile.Profile;
import org.hackystat.devcathlon.engine.profile.ProfileException;
import org.hackystat.devcathlon.engine.profile.ProfileManager;

/**
 * Provide Session state for the Profile area of the Devcathlon application. 
 * An instance of this is automatically created in DevcathlonSession. 
 * Access using DevcathlonSession.get().getProfileSession().
 * @author Philip Johnson, Scheller Sanchez
 */
public class ProfileSession implements Serializable {
  /** Profile model instance. */
  private Profile profile;
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Support session state for the Profile page and subpages. 
   */
  public ProfileSession() {
    //Nothing yet
  }
  
  /**
   * Sets profile given the user's email.
   * @param email user's email
   */
  public void setProfile(String email) {
    User currentUser = UserManager.getInstance().getUser(email);
    if (currentUser == null) {
      try {
        currentUser = UserManager.getInstance().addUser("", "", 
                            email, DevcathlonSession.get().getPassword());
          Profile profile = ProfileManager.getInstance().addProfile(currentUser, 
                                               "", "location", "###-####", "Your bio.", "");
//          currentUser.setProfile(profile);
          this.profile = profile; //currentUser.getProfile();
      }
//      catch (UserException e) {
//        System.out.println("UserException error");
//        e.printStackTrace();
//      }
      catch (ProfileException e) {
        e.printStackTrace();
      }
    } 
    else if (currentUser.getProfile() == null) {
      try {
        Profile profile = ProfileManager.getInstance().addProfile(currentUser, 
                            "", "", "", "", "");
//        currentUser.setProfile(profile);
        this.profile = profile;
      } 
      catch (ProfileException e) {
        e.printStackTrace();
      }
    }
    else {
      this.profile = currentUser.getProfile();
    }
  }
  
  /**
   * Retrieves the Profile model.
   * @return profile model instance
   */
  public Profile getProfile() {
    if (this.profile == null) {
      setProfile(DevcathlonSession.get().getEmail()); 
    }
    return this.profile;
  }
  
  /**
   * Returns list of teams associated to given user.
   * @return list of teams associated to user
   */
  public List<Team> getTeams() {
    return TeamManager.getInstance().getTeams(profile.getUser().getEmail());
  }
  
  /**
   * Set profile bio.
   * @param bio the user's bio
   */
  public void setBio(String bio) {
    profile.setBioInfo(bio);
  }
  
  /**
   * Get profile bio.
   * @return profile bio.
   */
  public String getBio() {
    return profile.getBioInfo();
  }
  
  /**
   * Set profile first name.
   * @param fName the user's first name.
   */
  public void setFirstName(String fName) {
    profile.setFirstName(fName);
  }
  
  /**
   * Set profile last name.
   * @param lName the user's first name.
   */
  public void setLastName(String lName) {
    profile.setLastName(lName);
  }
  
  /**
   * Set profile location.
   * @param location the user's location.
   */
  public void setLocation(String location) {
    profile.setLocation(location);
  }
  
  /**
   * Set contact.
   * @param contact the user's contact info.
   */
  public void setContact(String contact) {
    profile.setContact(contact);
  }
  
  /**
   * Set screenName.
   * @param screenName the user's contact info.
   */
  public void setScreenName(String screenName) {
    profile.setScreenName(screenName);
  }
  
  /**
   * Set user gender.
   * @param gender the user's first name.
   */
  public void setGender(String gender) {
    profile.setGender(gender);
  }
  
}
